package service

import (
	"context"
	"gquickstarter/internal/domain"
	"gquickstarter/internal/repository"
	"gquickstarter/pkg/logger"
	"gquickstarter/pkg/units"
)

type IAccountService interface {
	RegisterByEmail(ctx context.Context, email string, password string) error
}

type AccountService struct {
	store  repository.IAccountRepository
	store2 repository.IEmailRepository
	logger logger.LoggerWriter
}

func CreateAccountService(
	store repository.IAccountRepository,
	store2 repository.IEmailRepository,
	logger logger.LoggerWriter) IAccountService {
	return &AccountService{
		store:  store,
		store2: store2,
		logger: logger,
	}
}

func (asc *AccountService) RegisterByEmail(ctx context.Context, email string, password string) error {

	err := asc.store2.CreateEmailCheckCode(ctx, email, units.GenValidateCode(6))
	if err != nil {
		asc.logger.Error(err.Error())
	}
	//todo 检查email 格式
	asc.logger.Info("Email注册", logger.Field{
		Key:   "email",
		Value: email,
	})

	return asc.store.Create(ctx, domain.Account{
		Email:    email,
		Password: password})

}
